package com.example.controller;

import com.example.entity.Driver;
import com.example.entity.Vehicle;
import com.example.entity.VehicleType;
import com.example.service.DriverService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/drivers")
public class DriverController {

    @Autowired
    private DriverService driverService;

    @Operation(summary = "司机加盟申请", description = "处理司机的加盟申请，检查信息并保存到数据库")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "申请成功或失败信息",
                    content = @Content(schema = @Schema(implementation = String.class)))
    })
    @PostMapping
    public String applyForJoining(@RequestBody Map<String, Object> request) {
        //Map<String, Object> request 是一个 Map 对象，用于存储从请求体中获取的司机和车辆信息。
        String name = request.get("name").toString();
        String licenseNumber = request.get("licenseNumber").toString();
        String vehicleLicensePlate = request.get("vehicleLicensePlate").toString();
        String model = request.get("model").toString();
        String typeStr = request.get("type").toString();
        VehicleType type = VehicleType.valueOf(typeStr);
        int seats = Integer.parseInt(request.get("seats").toString());
        boolean canCarpool = Boolean.parseBoolean(request.get("canCarpool").toString());


        Vehicle vehicle = new Vehicle();
        vehicle.setLicensePlate(vehicleLicensePlate);
        vehicle.setModel(model);
        vehicle.setType(type);
        vehicle.setSeats(seats);
        vehicle.setCanCarpool(canCarpool);
        vehicle.setIsAutonomous(false); // 默认非自动驾驶
        vehicle.setLatitude(0); // 初始经纬度
        vehicle.setLongitude(0);
        Driver driver = new Driver(name, licenseNumber,vehicle);

        return driverService.applyForJoining(driver, vehicle);
    }
}